package com.chengliang0315.locationtest;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.chengliang0315.locationtest.util.LogUtils;
import com.chengliang0315.locationtest.util.location.GPSLocation;
import com.chengliang0315.locationtest.util.location.LBSLocation;
import com.chengliang0315.locationtest.util.location.LocationInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends AppCompatActivity {


    private static final int PERMISSIONS_FINE_LOCATION = 1;

    @BindView(R.id.txt_location_gps_msg)
    TextView txtLocationGpsMsg;
    @BindView(R.id.btn_location_gps)
    Button btnLocationGps;
    @BindView(R.id.txt_location_lbs_msg)
    TextView txtLocationLbsMsg;
    @BindView(R.id.btn_location_lbs)
    Button btnLocationLbs;
    private GPSLocation gpsLocation;
    private LBSLocation lbsLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        startService(new Intent(this, LocationService.class));

        LogUtils.e(ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)+"");
        LogUtils.e(ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION)+"");
        //检查定位权限是否已经允许
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            //申请定位权限
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_FINE_LOCATION);
        } else {
            initLocation();
            location();
        }
    }

    /**
     * 初始化定位需要的工具
     */
    private void initLocation() {
        gpsLocation = GPSLocation.getInstance(getApplication());
        gpsLocation.setOnGPSLocationRequestListener(new GPSLocation.OnGPSLocationRequestListener() {
            @Override
            public void onSucceed(LocationInfo response) {
                txtLocationGpsMsg.setText(response.toString());
            }

            @Override
            public void onFailed() {
                Toast.makeText(MainActivity.this, "GPS定位失败", Toast.LENGTH_SHORT).show();
            }
        });

        lbsLocation = LBSLocation.getInstance(getApplication(), new LBSLocation.OnLBSLocationRequestListener() {
            @Override
            public void onSucceed(LocationInfo response) {
                txtLocationLbsMsg.setText(response.toString());
            }

            @Override
            public void onFailed() {
                Toast.makeText(MainActivity.this, "LBS定位失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 启动定位
     */
    private void location() {
        Location location = gpsLocation.getLocation();
        gpsLocation.requestLocation();
        if (location == null) {      //如果GPS定位失败，就是用LBS定位
            lbsLocation.request();  //请求lbs定位
        }
    }

    @OnClick({R.id.btn_location_gps, R.id.btn_location_lbs})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_location_gps:
                gpsLocation.requestLocation();
                break;
            case R.id.btn_location_lbs:
                lbsLocation.request();
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSIONS_FINE_LOCATION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    initLocation();
                    location();
                } else {
                    Toast.makeText(this, "请打开定位权限", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

}
